package com.yyfax.pay.depository.controller.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.commons.json.FastJsonUtil;
import com.yyfax.framework.common.OpResponse;
import com.yyfax.framework.filter.CacheHttpServletRequestWrapper;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.depository.service.inner.FuiouChangeCardService;
import com.yyfax.pay.depository.service.inner.FuiouDepositFileInnerService;
import com.yyfax.pay.third.model.to.file.DownloadNotifyReq;
import com.yyfax.pay.third.model.to.file.QueryNotifyReq;
import com.yyfax.pay.third.model.to.file.UploadNotifyReq;
import com.yyfax.pay.third.model.to.fuioudeposit.UploadFileResultPushReq;

/**
 * 文件入口
 * 
 * @author buyi
 * @date 2017-09-05 17:34:34
 * @since v2.0.0
 */
@RestController
public class FileResultApiController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private FuiouDepositFileInnerService fuiouDepositFileInnerService;

	@Resource
	private FuiouChangeCardService fuiouChangeCardService;

	/**
	 * 上传文件结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:42:22
	 * @since v2.0.0
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/upload/result/{merchantCode}")
	public OpResponse tradeUploadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody UploadNotifyReq req,
			HttpServletRequest request) {
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验证签名
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_TRADE);
			// 解析上传结果
			fuiouDepositFileInnerService.parseUploadResultForFileServer(merchantCode, req);
			return OpResponse.success();
		} finally {
			body = null;
		}

	}

	/**
	 * 下载文件结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:44:40
	 * @since v2.0.0
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/download/result/{merchantCode}")
	public OpResponse tradeDownloadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody DownloadNotifyReq req,
			HttpServletRequest request) {
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验签
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_TRADE);
			fuiouDepositFileInnerService.parseDownloadResultForFileServer(merchantCode, req);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 回盘文件结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-10-09 17:08:29
	 * @since v2.0.0
	 * @param merchantCode
	 * @param req
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/query/result/{merchantCode}")
	public OpResponse tradeQueryResult(@PathVariable("merchantCode") String merchantCode, @RequestBody QueryNotifyReq req,
			HttpServletRequest request) {
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验签
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_TRADE);

			fuiouDepositFileInnerService.parseQueryNotify(merchantCode, req, PayTypeEnum.FILE_TRADE);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 报备文件上传结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:42:22
	 * @since v2.0.0
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/upload/result/{merchantCode}")
	public OpResponse checkUploadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody UploadNotifyReq req,
			HttpServletRequest request) {
		// 验签
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验证签名
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_CHECK);
			// 解析上传结果
			fuiouDepositFileInnerService.parseUploadResultForFileServer(merchantCode, req);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 报备文件下载结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:44:40
	 * @since v2.0.0
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/download/result/{merchantCode}")
	public OpResponse checkDownloadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody DownloadNotifyReq req,
			HttpServletRequest request) {
		// 验签
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();

		try {
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_CHECK);
			fuiouDepositFileInnerService.parseDownloadResultForFileServer(merchantCode, req);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 报备回盘文件结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-10-09 17:08:29
	 * @since v2.0.0
	 * @param merchantCode
	 * @param req
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/query/result/{merchantCode}")
	public OpResponse checkQueryResult(@PathVariable("merchantCode") String merchantCode, @RequestBody QueryNotifyReq req,
			HttpServletRequest request) {
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验签
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_CHECK);

			fuiouDepositFileInnerService.parseQueryNotify(merchantCode, req, PayTypeEnum.FILE_CHECK);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 交易文件上传结果推送<br>
	 * 该通知来自富友，这个接口是定制化接口，没有验签，生产环境需要配置白名单
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:47:08
	 * @since v2.0.0
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/upload/result/push/{merchantCode}")
	public String tradePushFileResult(@PathVariable("merchantCode") String merchantCode, HttpServletRequest request) {
		String json = null;
		UploadFileResultPushReq req = null;
		try {
			json = request.getParameter("json");
			logger.info("交易文件上传处理结果推送,请求参数json:{}", json);
			req = FastJsonUtil.fromJson(json, UploadFileResultPushReq.class);
			// 该接口为富友上传文件处理结果异步推送，没有订单概念，无法跟踪商户,所以在富友后台配置的时候，路径上直接拼装商户号
			return fuiouDepositFileInnerService.parseUploadPushResult(merchantCode, req, PayTypeEnum.FILE_TRADE);
		} finally {
			json = null;
			req = null;
		}
	}

	/**
	 * 报备文件上传结果推送<br>
	 * 改通知来自富友，这个接口是定制化接口，没有验签，生产环境需要配置白名单
	 * 
	 * @author buyi
	 * @date 2017-09-22 14:01:59
	 * @since v2.0.0
	 * @param merchantCode
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/upload/result/push/{merchantCode}")
	public String checkPushFileResult(@PathVariable("merchantCode") String merchantCode, HttpServletRequest request) {
		String json = null;
		UploadFileResultPushReq req = null;
		try {
			json = request.getParameter("json");
			logger.info("报备文件上传处理结果推送,请求参数json:{}", json);
			req = FastJsonUtil.fromJson(json, UploadFileResultPushReq.class);
			return fuiouDepositFileInnerService.parseUploadPushResult(merchantCode, req, PayTypeEnum.FILE_CHECK);
		} finally {
			json = null;
			req = null;
		}
	}

	/**
	 * 对账文件下载结果通知<br>
	 * 该通知来自文件系统
	 * 
	 * @author buyi
	 * @date 2017-10-10 14:42:59
	 * @since v2.0.0
	 * @param merchantCode
	 * @param req
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/account/download/result/{merchantCode}")
	public OpResponse accountDownloadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody DownloadNotifyReq req,
			HttpServletRequest request) {
		// 验签
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();

		try {
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_ACCOUNT);
			fuiouDepositFileInnerService.parseDownloadResultForFileServer(merchantCode, req);
			return OpResponse.success();
		} finally {
			body = null;
		}
	}

	/**
	 * 更换手机上传图片结果通知<br>
	 * 如果图片上传成功，则触发更换银行卡请求
	 * 
	 * @author buyi
	 * @date 2018-03-30 08:55:21
	 * @since v2.1.3
	 * @param merchantCode
	 * @param req
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/image/upload/result/{merchantCode}")
	public OpResponse changeCardImageUploadResult(@PathVariable("merchantCode") String merchantCode, @RequestBody UploadNotifyReq req,
			HttpServletRequest request) {
		String sign = request.getHeader(PayConstants.FIELD_SIGN);
		CacheHttpServletRequestWrapper cacheRequest = (CacheHttpServletRequestWrapper) request;
		String body = cacheRequest.getBodyContent();
		try {
			// 验证签名
			fuiouDepositFileInnerService.validFileServerSign(merchantCode, sign, body, PayTypeEnum.FILE_TRADE);
			// 解析上传结果
			fuiouChangeCardService.parseImageUploadResultForFileServer(merchantCode, req);

			return OpResponse.success();
		} finally {
			body = null;
		}
	}

}
